<?php
require_once ZEEGING_PATH."Controllers".DIRECTORY_SEPARATOR.'ApiBaseController.class.php';
require_once ZEEGING_PATH.'Librarys'.DIRECTORY_SEPARATOR.'ethereum'.DIRECTORY_SEPARATOR.'EthereumApi.php';
defined('IN_ZEEGING') or die('Access Denied');
class ApiController extends ApiBaseController{
	/*
	CREATE TABLE `address` (
	  `merchantid` varchar(50) NOT NULL COMMENT '商户唯一名',
	  `username` varchar(50) NOT NULL COMMENT '商户系统的用户名',
	  `privatekey` varchar(200) NOT NULL COMMENT '私钥',
	  `address` varchar(200) NOT NULL COMMENT '地址',
	  `status` int(11) NOT NULL DEFAULT '0' COMMENT '帐户状态,0为正常状态',
	  `currency` varchar(50) NOT NULL COMMENT '币名称',
	  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
	  PRIMARY KEY (`id`)
	) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='地址和私钥'

	CREATE TABLE `deposit` (
	  `amount` float NOT NULL COMMENT '订单金额rmb',
	  `paymentmethod` varchar(50) NOT NULL COMMENT '支付方式',
	  `status` varchar(20) NOT NULL COMMENT '状态0 = inactive, 100 = failed, 200 = completed',
	  `username` int(100) NOT NULL COMMENT '用户唯一标识',
	  `createtime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '订单创建时间',
	  `updatetime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '最后更新时间',
	  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
	  PRIMARY KEY (`id`)
	) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='充值订单表'
	*/
	public function getAddressAction(){
		$username=$this->POST('username');
		$merchantId=$this->POST('merchantid');
		// $currency=$this->GET('currency');
		$currency='ETH';

		if(empty($username)){
			$this->responseError("Username can not be empty");
			return;
		}
		else if(empty($merchantId)){
			$this->responseError("MerchantID can not be empty");
			return;
		}

		$addressRecord=$this->loadModel('address')->load($username,$merchantId,$currency);

		// 返回
		if(!empty($addressRecord))
		{
			$this->response(array(
				"address"=>$addressRecord->address,
				"username"=>$username
			));
		}
		else{
			if(mysql_error())$this->responseError(mysql_error(),mysql_errno());
			else $this->responseError();
		}
		
	}
	public function getEtherFromRmbAction(){
		$rmbAmount=$this->POST('amount');

		if(empty($rmbAmount)){
			$this->responseError("Amount can not be empty");
			return;
		}
		$etherAmount=EthereumApi::toFloorFixed(EthereumApi::convertRmbToEther($rmbAmount),5);
		// 返回
		if(!empty($etherAmount))
		{
			$this->response(array(
				"ether"=>$etherAmount,
				"rmb"=>$rmbAmount
			));
		}
		else{
			$this->responseError();
		}

	}
	public function getPriceAction(){
		$price=EthereumApi::realtimePrice();
		// 返回
		if(!empty($price))
		{
			$this->response(array(
				"price"=>$price
			));
		}
		else{
			$this->responseError();
		}
	}
	public function testAction(){
		phpinfo();
	}
	public function getMayaAlipayDepsoitParametresAction(){
		$config=$this->loadConfigs('mayapay');
		$total_fee=$this->POST('total_fee');
		// $total_fee=1;
		if(!empty($total_fee)){
			$order=$this->loadModel('MayaDepositOrder');
			$order->amount=number_format($total_fee,2,'.','');
			$order->paymentmethod='maya_alipaywap';
			$order->status=0;
			$order->username=$this->POST('mobile');
			if($order->save()){
				$param=array(
					"version"=>'1.0',
					"customerid"=>$config['userid'],
					"userkey"=>$config['userkey'],
					"sdorderno"=>$order->id,
					"total_fee"=>$order->amount,	// 支付金额包含手续费
					"paytype"=>'alipaywap',//$this->POST('paytype'),
					"bankcode"=>'ICBC',//$this->POST('bankcode'),
					"notifyurl"=>$config['notifyurl'],
					"returnurl"=>$config['returnurl'],
					"remark"=>'',
					"get_code"=>0,//$this->POST('get_code'),	// 微信选项 0-默认 1-二维码
				);
				$param["sign"]=md5('version='.$param["version"].'&customerid='.$param["customerid"].'&total_fee='.$param["total_fee"].'&sdorderno='.$param["sdorderno"].'&notifyurl='.$param["notifyurl"].'&returnurl='.$param["returnurl"].'&'.$param["userkey"]);
				$this->response($param);
			}
			else{
				$this->responseError();
			}
			
		}
		else{
			$this->responseError("Amount can not be empty");
		}
	}
	public function getXunjieDepositParametresAction(){
		$config=$this->loadConfigs('xunjiepay');
		$total_fee=1;//$this->POST('total_fee');
		// $total_fee=1;
		if(!empty($total_fee)){
			$order=$this->loadModel('RealMoneyDepositOrder');
			$order->amount=number_format($total_fee,2,'.','');
			$order->paymentmethod='xunjie_unionpay';
			$order->status=0;
			$order->username=$this->POST('mobile');
			if($order->save()){
				$param=array(
				    "pay_memberid" => $config['memberid'],
				    "pay_orderid" => $order->id,
				    "pay_amount" => $order->amount,
				    "pay_applydate" => date("Y-m-d H:i:s"),  //订单时间
				    "pay_bankcode" => "911", //支付宝扫码  //商户后台通道费率页 获取银行编码
				    "pay_notifyurl" => $config['notifyurl'],
				    "pay_callbackurl" => $config['returnurl']
				);

				ksort($param);
				$md5str = "";
				foreach ($param as $key => $val) {
				    $md5str = $md5str . $key . "=" . $val . "&";
				}
				//echo($md5str . "key=" . $Md5key);
				$sign = strtoupper(md5($md5str . "key=" . $config['userkey']));
				$param["pay_md5sign"] = $sign;
				$param['pay_attach'] = "1234|456";
				$param['pay_productname'] ='博猫体育';
				$this->response($param);
			}
			else{
				$this->responseError();
			}
			
		}
		else{
			$this->responseError("Amount can not be empty");
		}
	}

	public function getStarFireDepositParametresAction(){
		$config=$this->loadConfigs('starfireotc');
		$total_fee=$this->POST('total_fee');
		// $total_fee=1;
		if(!empty($total_fee)){
			$order=$this->loadModel('RealMoneyDepositOrder');
			$order->amount=number_format($total_fee,2,'.','');
			$order->paymentmethod='starfire_otc';
			$order->status=0;
			$order->username=$this->POST('mobile');
			if($order->save()){
				$param=array(
				    "APPKey" => $config['apikey'],
				    "outOrderId" => $order->id,
				    "customerAmountCny" => $order->amount,
				    "signType" => "MD5", 
				    "receiveUrl" => $config['notifyurl'],
				    "pickupUrl" => $config['returnurl']
				);
				$sign = md5($param['outOrderId'].$param['pickupUrl'].$param['receiveUrl'].$param['customerAmountCny'].$param['signType'].$config['md5key']);
				$param["sign"] = $sign;
				$this->response($param);
			}
			else{
				$this->responseError();
			}
			
		}
		else{
			$this->responseError("Amount can not be empty");
		}
	}
}
//------------------------------------------------
//End of File : /Controllers/IndexController.class.php
//------------------------------------------------
